package com.demo.gather;

import java.io.BufferedReader;
import java.io.FileReader;
import java.sql.Date;
import java.util.Collection;
import java.util.List;
import java.util.Vector;

import com.demo.bean.Enviroment;
import com.demo.util.FileBackup;
import com.demo.util.FileNameEnums;

public class GatherImpl implements IGather {

	/**
	 * 当第一次解析结束，就不用去解析上一次解析过得数据
	 * reader.skip(n)
	 * 
	 * gather只是用来解析文档，不应该具备将数据备份
	 * 所以我们需要将备份的代码抽取出来，放到一个新类中
	 * @throws Exception 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public Collection<Enviroment> gather(){
		//用来记录字节数
		long count = 0;
		List<Enviroment> list = new Vector<Enviroment>();
		try {
			String fileName = FileNameEnums.CLIENT_ANALYSISFILE_PATH.getPath();
			BufferedReader in = new BufferedReader(new FileReader(fileName ));
			
			
			
			//跳过多少字符
			
			Object object = FileBackup.recover(FileNameEnums.CLIENT_COUNT_PATH.getPath(),true);
			if(object != null) {
				count += (Long)object;
			}
			
			in.skip(count);
			
			String line = null;
			Object oldData = FileBackup.recover(FileNameEnums.CLIENT_DATA_PATH.getPath(),true);
			if(oldData != null) {
				list.addAll((Collection<? extends Enviroment>)oldData);
			}
			while((line=in.readLine()) != null) {
				
				count += line.length()+2;
				
				String[] split = line.split("[|]");
				if(split.length == 9) {
					Enviroment en = null;
					if(Integer.parseInt(split[3]) == 16 && split[6].length() == 10) {
						int wInt = Integer.parseInt(split[6].substring(0, 4), 16);
						Double wDou = ((float)wInt*0.00268127)-46.85;
//						String data = "温度："+wDou+",湿度："+sDou;
						
						en = getEnviroment(split,"温度", wDou);
						list.add(en);
						
						int sInt = Integer.parseInt(split[6].substring(4, 8), 16);
						Double sDou = ((float)sInt*0.00190735)-6;
						en = getEnviroment(split, "湿度", sDou);
						list.add(en);
					}else if("256".equals(split[3])){
						int dataInt = Integer.parseInt((split[6].substring(0, 4)),16);
						Double data = (double)dataInt;
//						String data = "数据："+dataInt;
						en = getEnviroment(split, "光照强度", data);
						list.add(en);
					}else if("1280".equals(split[3])) {
						int dataInt = Integer.parseInt((split[6].substring(0, 4)),16);
						Double data = (double)dataInt;
//						String data = "数据："+dataInt;
						en = getEnviroment(split, "CO2", data);
						list.add(en);
					}
				}
			}
			//将记录的字节数存入文件
			FileBackup.store(FileNameEnums.CLIENT_COUNT_PATH.getPath(), count);
			
			in.close();
			return list;
		} catch (Exception e) {
			try {
				FileBackup.store(FileNameEnums.CLIENT_DATA_PATH.getPath(), list);
			} catch (Exception e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			return null;
		}
	}

	private Enviroment getEnviroment(String[] split,String name, Double data) {
		Enviroment en = new Enviroment(split[0], split[1], Long.parseLong(split[2]), name, 
				Long.parseLong(split[4]), Integer.parseInt(split[5]),data, 
				Integer.parseInt(split[7]), new Date(Long.parseLong(split[8])));
		return en;
	}

}
